﻿@model  RoleDTO[]
@{
    ViewBag.Title = "AddNew";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<section class="layui-larry-box">
    <div class="larry-personal">
        <header class="larry-personal-tit">
            <span>添加管理员</span>
        </header><!-- /header -->
        <div class="larry-personal-body clearfix">
            <form class="layui-form col-lg-5" id="form" method="post">

                <div class="layui-form-item">
                    <label class="layui-form-label">管理员姓名</label>
                    <div class="layui-input-block">
                        <input lay-verify="required" type="text" name="Name" autocomplete="off" class="layui-input" value="" placeholder="请输入管理员姓名">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-block">
                        <input type="text" name="NickName" lay-verify="required" autocomplete="off" value="" class="layui-input" placeholder="请输入管理员昵称">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">手机号码</label>
                    <div class="layui-input-block">
                        <input type="text" id="PhoneNum" name="PhoneNum" value="" lay-verify="phone" autocomplete="off" class="layui-input" placeholder="输入手机号码">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" id="Email" name="Email" value="" lay-verify="email" autocomplete="off" class="layui-input" placeholder="输入邮箱">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="Gender" value="true" title="男" checked="" /><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span>男</span></div>

                        <input type="radio" name="Gender" value="false" title="女" />
                        <div class="layui-unselect layui-form-radio">
                            <i class="layui-anim layui-icon"></i>
                            <span>女</span>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">添加头像</label>
                    <div class="layui-input-block">
                        <input type="file" title="添加头像" name="file" class="layui-upload-file">
                    </div>
                    <input type="hidden" id="ImgSrc" name="ImgScr" lay-verify="upload" value="" />

                    <div class="layui-input-block">
                            <img src="#" id="img" alt="请选择图片" height="50px;"  />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="password" id="PassWord" name="PassWord" value="" lay-verify="pass" autocomplete="off" class="layui-input" placeholder="输入密码">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        @foreach (var role in Model)
                        {
                            <input type="checkbox" name="Roles" value="@role.Id" title="@role.Name" />
                        }
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</section>

@section FooterScript{
    <script type="text/javascript">
        layui.use(['form', 'upload', 'jquery'], function () {
            var form = layui.form();
            layui.upload({
                url: '/AdminUser/Upload',//上传接口
                success: function (res) {
                    if (res.status=="error") {
                        layer.msg(res.errorMsg, {icon:2,time:1000});
                    } else if (res.status == "ok") {
                        layer.msg("上传成功");
                        console.log(res.data);
                        $('#ImgSrc').attr("value", res.data);
                        $('#img').attr("src","http://static.amortal.top/"+res.data);
                    } else if (res.status == "redirect") {
                        location.href = res.data;
                    } else {
                        layer.msg("错误");
                    }
                }
            });

            form.on("submit(demo1)", function (data) {
                var json = $('#form').serialize();
                console.log(json);
                $.ajax({
                    url: "/AdminUser/AddNew",
                    data: json,
                    dataType: "json",
                    type: "post",
                    success: function (res) {
                        if (res.status == "ok") {
                            parent.location.reload();
                        } else if (res.status == "redirect") {
                            location.href = res.msg;
                        } else if (res.status == "error") {
                            appConfig.layer.msg(res.errorMsg, { time: 1000, icon: 1 });
                        } else {
                            appConfig.layer.msg("错误", { time: 1000, icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("请求出错");
                    }
                })

                return false;
            })

            form.verify({
                upload:function (val,item) {
                    if(val==""){
                        return '请选择图片';
                    }
                },
                pass: [
                    /^[\S]{6,12}$/,
                    '密码必须6到12位，且不能出现空格'
                ]
            });

        });
        var appConfig = {};

    </script>
}




